class: center, middle, inverse, title-slide .title[ # ggplot2 ] .subtitle[ ## Ein Überblick ] .author[ ### Andreas Filser ] .date[ ### Frühjahr 2025 ] --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 90%} /*font size for manual panels*/ code, pre { font-weight: 500; /* Set font weight to normal */ } .blade1 { font-size: "80%"; /* 80% of the parent's font size */ } </style> # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + * aes(x = jahr) ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + * aes(y = anz_baeume) ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + * geom_point() ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + * aes(color = baumart) ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + * scale_color_manual(values=c("red","green4")) ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + * labs(title="Wie echt sind deine Blätter?") ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + labs(title="Wie echt sind deine Blätter?") + * labs(subtitle="Verkaufte Weihnachtsbäume in USA | Quelle: Statista") ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + labs(title="Wie echt sind deine Blätter?") + labs(subtitle="Verkaufte Weihnachtsbäume in USA | Quelle: Statista") + * labs(y = "Anzahl verkaufte Bäume (in Mio)") ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + labs(title="Wie echt sind deine Blätter?") + labs(subtitle="Verkaufte Weihnachtsbäume in USA | Quelle: Statista") + labs(y = "Anzahl verkaufte Bäume (in Mio)") + * labs(x = "Jahr") ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + labs(title="Wie echt sind deine Blätter?") + labs(subtitle="Verkaufte Weihnachtsbäume in USA | Quelle: Statista") + labs(y = "Anzahl verkaufte Bäume (in Mio)") + labs(x = "Jahr") + * labs(color = "") ``` ] .panel2-manual[  ] --- # Weihnachtsbäume .panel1-manual[ ``` r ggplot(data = christmas_trees) + aes(x = jahr) + aes(y = anz_baeume) + geom_point() + aes(color = baumart) + scale_color_manual(values=c("red","green4")) + labs(title="Wie echt sind deine Blätter?") + labs(subtitle="Verkaufte Weihnachtsbäume in USA | Quelle: Statista") + labs(y = "Anzahl verkaufte Bäume (in Mio)") + labs(x = "Jahr") + labs(color = "") + theme_minimal() ##< ``` ] .panel2-manual[  ] --- # Linien statt Punkte .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color=baumart)) + * geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` Wir können durch Veränderung des `geom_...` schnell von einem Punkt- zu einem Liniendiagramm wechseln. Wir tauschen einfach `geom_point` durch `geom_line` aus. ] .panel2-manual[  ] --- # Linien **und** Punkte .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_point() + * geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # Weitere Optionen <!-- .blade1[Für `geom_line`und `geom_point` gibt es eine Vielzahl an Anpassungsmöglichkeiten] --> .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_point(shape = 17, size = 3) + * geom_line(linetype = "dashed", size = .25) + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` Eine Übersicht zu allen `shape`s und `linetype`s findet sich bspw. [hier](http://www.cookbook-r.com/Graphs/Shapes_and_line_types/) ] .panel2-manual[  ] --- # Weitere Optionen in `aes`thetics .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart, * size = anz_baeume)) + geom_point(shape = 17) + geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` Die Größe der Punkte & Linien entspricht der Verkaufszahl. ] .panel2-manual[  ] --- # Legendentitel für `size` .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart, size = anz_baeume)) + geom_point(shape = 17) + geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "", * size = "Absatz (in Mio)") + theme_minimal() ``` ] .panel2-manual[  ] --- # `size` für nur ein geom_... .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_point(aes(size = anz_baeume),shape = 17) + geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "", size = "Absatz (in Mio)") + theme_minimal() ``` ] .panel2-manual[  ] --- # Anpassungen durch Variablen definieren .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_point(aes(size = anz_baeume, shape = baumart)) + geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "", size = "Absatz (in Mio)") + theme_minimal() ``` ] .panel2-manual[  ] --- # Anpassungen durch Variablen definieren .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + geom_point(aes(size = anz_baeume, shape = baumart)) + geom_line() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "", * shape = "Baumart", size = "Absatz (in Mio)") + theme_minimal() ``` ] .panel2-manual[  ] --- # Anpassungen durch Variablen definieren .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_point(aes(size = anz_baeume, shape = baumart)) + geom_line() + scale_color_manual(values = c("red", "green4")) + * scale_shape_manual(values = c(16,18)) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "", * shape = "", size = "Absatz (in Mio)") + theme_minimal() ``` ] .panel2-manual[  ] --- # Säulendiagramme .blade1[`geom_col` erstellt Säulendiagramme] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart)) + * geom_col() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # fill und color .blade1[`fill` ist für die Flächenfarben verantwortlich, zB die Farbe der Säulen] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, color = baumart, * fill = baumart)) + geom_col() + scale_color_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", color = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # scale_... .blade1[`scale_fill_manual` steuert die Werte für die Flächenfarben] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col() + * scale_fill_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # Säulen nebeneinander .blade1[Auch für `geom_col()` gibt es Anpassungsmöglichkeiten] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + * geom_col(position=position_dodge()) + scale_fill_manual(values = c("red", "green4")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # Legendenbildung .smaller[`breaks` steuert die Zuordnung von Farben und Ausprägungen, `legend.position` in `theme` platziert die Legende.] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col(position=position_dodge()) + scale_fill_manual(values = c("red", "green4"), * breaks = c("Fake tree", "Real tree")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() + theme(legend.position="bottom") ``` ] .panel2-manual[  ] --- # Legendenbildung .blade1[Auch die Beschriftung kann geändert werden] .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col(position=position_dodge()) + scale_fill_manual(values = c("red", "green4"), breaks = c("Fake tree", "Real tree"), * labels = c("künstl.", "echt")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() + theme(legend.position="top") ``` .smaller[Mehr zur Formatierung der Legende [hier](http://www.cookbook-r.com/Graphs/Legends_%28ggplot2%29)] ] .panel2-manual[  ] --- # Color Brewer .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col(position=position_dodge()) + * scale_fill_brewer(palette = "Accent") + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() ``` .smaller[Der Übersichtlichkeit wurde hier `color` weggelassen, es gibt natürlich auch `scale_color_brewer()`] ] .panel2-manual[  ] --- # Color Brewer .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col(position=position_dodge()) + * scale_fill_brewer(palette = "Paired") + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() ``` ] .panel2-manual[  ] --- # Color Brewer .panel1-manual[ ``` r ggplot(data = christmas_trees, aes(x=jahr,y=anz_baeume, fill = baumart)) + geom_col(position=position_dodge()) + scale_fill_brewer(palette = "Accent", * breaks = c("Fake tree", "Real tree"), * labels = c("künstl.", "echt")) + labs(title = "Wie echt sind deine Blätter?", subtitle = "Verkaufte Weihnachtsbäume in USA | Quelle: Statista", y = "Anzahl verkaufte Bäume (in Mio)", x = "Jahr", fill = "") + theme_minimal() ``` .smaller[`breaks` und `labels` funktionieren auch in `scale_color_brewer()`] ] .panel2-manual[  ]